<!-- 指令单价查询：蹇泽阳 -->
<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <div class="flex mt10" style="width: 100%; background-color: white">
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
        <h1 class="f16">指令查询</h1>
        <Table
          :tableHeader="tableHeader"
          :tableData="tableData"
          class="mt10"
          :isLoading="isLoading"
          @sortChange="sortChange"
          @currentRow="currentRow"
        />
        <PageModule
          :PageData="{ total, pageSizeShow: true, buttonS: true }"
          :layout="'prev, pager'"
          @pageSizeChange="handleSizeChange"
          @pageChange="handleCurrentChange"
        />
      </el-card>
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }" style="width: 100%">
        <h1 class="f16">指令单价明细档</h1>
        <Table :tableHeader="tableHeader2" :tableData="tableData2" class="mt10" :isLoading="isLoading2" :summaryData="summaryData2" summary />
      </el-card>
    </div>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import fileRequest from '@/utils/fileRequest'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'CommandUnitPriceQueryKa'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import type { formListType } from '@/components/Find/index.vue'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import service from '@/utils/request'
import { debounce } from 'lodash'
import { onMounted, watch } from 'vue'
const formList = ref<any>([
  {
    type: 'input',
    inputPlaceholder: '指令',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '型体',
    inputValue: ''
  },
  {
    type: 'date',
    dateLabel: '派工日期',
    dateValue: ''
  },
  {
    type: 'date',
    dateLabel: '检核日期',
    dateValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '类型',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'checkbox',
    checkboxLabel: '未检核',
    checked: true
  },
  {
    type: 'checkbox',
    checkboxLabel: '已检核',
    checked: false
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '导出'
  }
])

// 表一模块
// 表一按钮
const findClicked = async (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
  if (content === '导出') {
    const res = (await fileRequest.post(`/OrderUnitPrice/ExportLotNoList`, {
      color: '',
      dateEnd1: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '检核日期'
      )?.dateValue?.[0],
      dateEnd2: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '派工日期'
      )?.dateValue?.[1],
      dateStart1: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '派工日期'
      )?.dateValue?.[0],
      dateStart2: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '派工日期'
      )?.dateValue?.[1],
      flag: cfmFlag(),
      lotNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '指令'
      )?.inputValue,
      sort: '',
      sortWay: '',
      style: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '型体'
      )?.inputValue,
      type: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '类型')
        ?.selectValue,
      vendorName: '',
      pageNum: currentPage.value,
      pageSize: pageSize.value
    })) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`utf-8''`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
}
const tableData = ref([])
const isLoading = ref(true)
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}
// 表格计算函数
const cfmFlag = () => {
  const Unaudited = formList.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '未检核')
  const Audited = formList.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '已检核')
  if (!Unaudited || !Audited) return
  if (Unaudited.checked && Audited.checked) {
    return 0
  } else if (Unaudited.checked) {
    return 1
  } else if (Audited.checked) {
    return 2
  } else {
    ElMessage('必须至少选择一项')
    Unaudited.checked = true
    Audited.checked = true
    return 0
  }
}
const sortChange = (order: any, prop: any, column: any) => {
  let orderFlag = null
  if (order === null) {
    orderFlag = 1
  } else if (order === 'ascending') {
    orderFlag = 1
  } else if (order === 'descending') {
    orderFlag = 2
  }
  getTableList(orderFlag, prop)
}
// 获取表数据
const getTableList = debounce(async (orderFlag: any = '', orderName: string = '') => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>('/OrderUnitPrice/getLotNoList', {
    sortWay: orderFlag,
    sort: orderName,
    color: '',
    dateEnd1: formList.value.find((item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '检核日期')
      ?.dateValue?.[0],
    dateEnd2: formList.value.find((item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '派工日期')
      ?.dateValue?.[1],
    dateStart1: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '派工日期'
    )?.dateValue?.[0],
    dateStart2: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '派工日期'
    )?.dateValue?.[1],
    flag: cfmFlag(),
    lotNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '指令'
    )?.inputValue,
    style: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '型体'
    )?.inputValue,
    type: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '类型')
      ?.selectValue,
    vendorName: '',
    pageNum: currentPage.value,
    pageSize: pageSize.value
  })
  isLoading.value = false
  tableData.value = tableDataList.list.map((item: any) => {
    if (item.cfmFlag === 'Y') item.backgroundColor = '#00FF00'
    return item
  })
  total.value = tableDataList.total
  currentRow()
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
const tableHeader = ref([
  {
    sortable: 'column',
    backgroundColor: true,
    label: '指令',
    prop: 'lotNo'
  },
  {
    label: '型体',
    prop: 'style'
  },
  {
    label: '颜色',
    prop: 'color'
  },
  {
    label: '类型',
    prop: 'type'
  },
  {
    width: 70,
    label: '单价',
    prop: 'price'
  },
  {
    label: '检核标记',
    prop: 'cfmFlag'
  }
])
// 点击表给另一张表传数据
const currentTableRow = ref({}) as any
const currentRow = async (row: any = tableData.value[0] ? tableData.value[0] : {}) => {
  currentTableRow.value = row
  getTableList2()
}

// 表二模块
const tableData2 = ref([])
const isLoading2 = ref(true)
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData2 = ref<summaryDataType[]>([
  {
    index: 5,
    num: 0
  },
  {
    index: 6,
    num: 0
  }
])
// 分页器
const currentPage2 = ref(1)
const pageSize2 = ref(40)
const total2 = ref(0)
const handleSizeChange2 = (val: number) => {
  pageSize2.value = val
  getTableList2()
}
const handleCurrentChange2 = (val: number) => {
  currentPage2.value = val
  getTableList2()
}
// 获取表数据
const getTableList2 = debounce(async () => {
  isLoading2.value = true
  let tableDataList = await service.post<any, any>('/OrderUnitPrice/getLotNoPriceDetail', currentTableRow.value)
  isLoading2.value = false
  tableData2.value = tableDataList.list
  total2.value = tableDataList.total
  // currentRow2()
  summaryData2.value[0].num = tableDataList.countCutLayer
  summaryData2.value[1].num = tableDataList.count
}, 500)
const tableHeader2 = ref([
  {
    label: '类型',
    prop: 'type'
  },
  {
    width: 50,
    label: '序号',
    prop: 'seqNo'
  },
  {
    width: 120,
    label: '部位',
    prop: 'partName'
  },
  {
    width: 250,
    label: '材料名称',
    prop: 'MtlName'
  },
  {
    label: '片数',
    prop: 'pieceNum'
  },
  {
    label: '层数',
    prop: 'cutLayer'
  },
  {
    label: '单价',
    prop: 'price'
  },
  {
    label: '备注',
    prop: 'reMark'
  }
])

const getSelectList = async () => {
  formList.value
    .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '类型')
    ?.selectOptions?.push(
      {
        optionKey: `冲裁`,
        optionLabel: `冲裁`,
        optionValue: `冲裁`
      },
      {
        optionKey: `手工`,
        optionLabel: `手工`,
        optionValue: `手工`
      },
      {
        optionKey: `针车`,
        optionLabel: `针车`,
        optionValue: `针车`
      },
      {
        optionKey: `全部`,
        optionLabel: `全部`,
        optionValue: ``
      }
    )
}
onMounted(() => {
  getSelectList()
})
</script>

<style lang="less" scoped></style>
